package com.etc.controller;

import com.etc.entity.Permission;
import com.etc.entity.Role;
import com.etc.entity.User;
import com.etc.entity.UserFenye;
import com.etc.service.RoleService;
import com.etc.service.ShiorService;
import com.github.pagehelper.PageHelper;
import org.apache.log4j.Logger;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    @Qualifier(value = "shior")
    private ShiorService shiorService;
    private Logger logger = Logger.getLogger(HelloController.class);
    @Autowired
    @Qualifier(value = "roles")
    private RoleService roleService;


    @RequestMapping("/hello")
    public String sayHello(HttpSession session) {
        System.out.println("hello,springmvc");
        User admin = shiorService.FindByusername("admin");
        System.out.println(admin);
        User admin1 = shiorService.LoginSuccess("admin", "123");
        System.out.println(admin1);
        List<Permission> permissionList = shiorService.FindPermission("admin") ;
        System.out.println(permissionList);
        logger.info("info ");
         User user= (User)session.getAttribute("user");
        System.out.println(user.getRoleid().getRolename());

        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "/chinesemap/{pagenum}",method = RequestMethod.POST)
    public UserFenye chars(Model model, @PathVariable("pagenum") String pagenum){
        UserFenye userFenye = new UserFenye();
      userFenye.setPageNum(Integer.valueOf(pagenum));
          try {
              Thread.sleep(5000);
          }catch (InterruptedException e){
              e.printStackTrace();
              logger.info("定时任务完成");
          }

        return userFenye;
    }


    @RequestMapping(value = "/roles/{pagenum}",method = RequestMethod.GET)
    public String rolesmanager(Model model, @PathVariable(value = "pagenum",required = false) String pagenum){
        System.out.println("进入方法"+pagenum);
        //每页长度
        int pagesize=5;
        //当前页
        int pagecurrent=1;
        //查询数据库总数据
        int counttotal=shiorService.quaryall().size();
        //判断总页数
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=counttotal/pagesize+1;
        }
        //判断当前页
        if(pagenum==null){
            pagecurrent=1;
        }else {
            pagecurrent=Integer.valueOf(pagenum);
        }
     //首页每页判断
        if(pagecurrent<=1){
            pagecurrent=1;
        }else if(pagecurrent>=pages){
            pagecurrent=pages;
        }
        //分页start
        int pagestart=0;
        pagestart=(pagecurrent-1)*pagesize;

        //获取角色列表
        List<Role> roles = shiorService.roles();



        //根据当前页和页面尺度进行分页
        PageHelper.startPage(pagecurrent,pagesize);
        List<User> users = shiorService.quaryall();
        System.out.println(users);
        //加入角色列表信息
        model.addAttribute("users",users);
        //加入总页数
        model.addAttribute("pages",pages);
        //加入当前页
        model.addAttribute("pagecurrent",pagecurrent);
        //加入查询总条数
        model.addAttribute("counttotal",counttotal);
        //加入每页宽度
        model.addAttribute("pagesize",pagesize);
        //角色列表存入
        model.addAttribute("roles",roles);
        //返回页面
        return "permission";
    }
     //user为空的
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public String search(Model model,User user){
        System.out.println(user+"nihao");
        logger.info(user+"搜索");
        String pagenum=null;
        //位用户对象加入角色
        Role role =roleService.getrole(user.getRoleone());
        user.setRoleid(role);
        user.setUsername(user.getUsername());
        //每页长度
        int pagesize=5;
        //当前页
        int pagecurrent=1;
        //查询数据库数据量
        int counttotal=shiorService.userlist(user).size();
        //判断总页数
        int pages=1;
        if(counttotal/pagesize==0){
            pages=1;
        }else {
            pages=counttotal/pagesize+1;
        }
        //判断当前页
        if(pagenum==null){
            pagecurrent=1;
        }else {
            pagecurrent=Integer.valueOf(pagenum);
        }
        //首页每页判断
        if(pagecurrent<=1){
            pagecurrent=1;
        }else if(pagecurrent>=pages){
            pagecurrent=pages;
        }
        //分页start
        int pagestart=0;
        pagestart=(pagecurrent-1)*pagesize;

        //获取角色列表
        List<Role> roles = shiorService.roles();
        System.out.println("roles"+roles);



        //根据当前页和页面尺度进行分页
        PageHelper.startPage(pagecurrent,pagesize);
        List<User> users = shiorService.userlist(user);
        System.out.println(users);
        //加入角色列表信息
        model.addAttribute("users",users);
        //加入总页数
        model.addAttribute("pages",pages);
        //加入当前页
        model.addAttribute("pagecurrent",pagecurrent);
        //加入查询总条数
        model.addAttribute("counttotal",counttotal);
        logger.info("counttotal"+counttotal);
        //加入每页宽度
        model.addAttribute("pagesize",pagesize);
        //角色列表存入
        model.addAttribute("roles",roles);
        //查询对象
        model.addAttribute("searchuser",user);



        //返回页面
        return "permission";
    }

    @ResponseBody
    @RequestMapping(value = "/UserModify",method = RequestMethod.POST)
    public User UserModify(@RequestBody User user){
        logger.info(user+"记录");
        Role role =roleService.getrole(user.getRoleone());
        user.setRoleid(role);
        shiorService.update(user);
        return user;
    }

    //删除用户
    @RequestMapping(value = "/delete/{userid}",method = RequestMethod.GET)
    public String Userdelete(@PathVariable("userid") String userid, Model model){
        shiorService.delete(Integer.valueOf(userid));
        return "forward:/hello/roles/1";
    }

    @ResponseBody
    @RequestMapping(value="/showmodify/{userid}",method = RequestMethod.GET)
    public User showmodify(@PathVariable(value = "userid",required = false) String userid){
        return shiorService.FindUser(Integer.valueOf(userid));
    }

    @RequestMapping(value="/saveuser",method = RequestMethod.GET)
    public String  usersave(User user){
        logger.info(user);
        logger.info(user.getUsername());
        String rolename = user.getRoleone();
        Role rolebyname = shiorService.rolebyname(rolename);
        user.setRoleid(rolebyname);user.setPdate(new Date());user.setPassword(new Md5Hash("123","abc").toHex());
         shiorService.register(user);
         return "forward:/hello/roles/1";
    }



}
